<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 人员管理逻辑
 */
class Personnel extends AdminBase
{
    
    /**
     * 获取员工信息
     */
    public function getPersonnelInfo($where = [], $field = true)
    {
        
        $info = $this->modelPersonnel->getInfo($where, $field);
        
        $info['name'] = $this->modelPersonnel->getValue(['id' => $info['id']], 'name');
        
        return $info;
    }


    /**
     * 获取人员列表
     */
    public function getPersonnelList($where = [], $field = 'p.*', $order = '', $paginate = DB_LIST_ROWS)
    {
        $this->modelPersonnel->alias('p');
        return $this->modelPersonnel->getList($where, $field, $order, $paginate);
    }

     /**
     * 导出员工列表
     */
    public function exportPersonnelList($where = [], $field = 'p.*', $order = '')
    {
        
        $list = $this->getPersonnelList($where, $field, $order, false);
        
        $titles = "员工名,邮箱,手机,注册时间,";
        $keys   = "name,email,mobile,create_time,";
        
        action_log('导出', '导出员工列表');
        
        export_excel($titles, $keys, $list, '员工列表');
    }
    

     /**
     * 获取员工列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['p.name|p.email|p.mobile'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
   
    
    /**
     * 人员管理信息编辑
     */
    public function personnelEdit($data = [])
    {
        
        $validate_result = $this->validatePersonnel->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePersonnel->getError()];
        }
        
        $url = url('personnelList');
        
        $result = $this->modelPersonnel->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '人员管理' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelPersonnel->getError()];
    }

     /**
     * 会员添加
     */
    public function personnelAdd($data = [])
    {
        
        $validate_result = $this->validatePersonnel->scene('add')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePersonnel->getError()];
        }
        
        $url = url('personnelList');
        
        
        $data['leader_id'] = MEMBER_ID;
        $data['is_inside'] = DATA_NORMAL;
        
        $data['password'] = data_md5_key($data['password']);
        
        $result = $this->modelPersonnel->setInfo($data);
        
        $result && action_log('新增', '新增会员，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '会员添加成功', $url] : [RESULT_ERROR, $this->modelPersonnel->getError()];
    }

     /**
     * 修改密码
     */
    public function editPassword($data = [])
    {
        
        $validate_result = $this->validatePersonnel->scene('password')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validatePersonnel->getError()];
        }
        
        $personnel = $this->getPersonnelInfo(['id' => $data['id']]);
        
        if (data_md5_key($data['old_password']) != $personnel['password']) {
            
            return [RESULT_ERROR, '旧密码输入不正确'];
        }
        
        $data['id'] = MEMBER_ID;
        
        $url = url('index/index');
        
        $result = $this->modelPersonnel->setInfo($data);
        
        $result && action_log('编辑', '会员密码修改，id：' . $data['id']);
        
        return $result ? [RESULT_SUCCESS, '密码修改成功', $url] : [RESULT_ERROR, $this->modelPersonnel->getError()];
    }

    
    /**
     * 人员管理删除
     */
    public function personnelDel($where = [])
    {
        
        $result = $this->modelPersonnel->deleteInfo($where);
        
        $result && action_log('删除', '人员管理删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelPersonnel->getError()];
    }
}
